package com.spdg.ring.task;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import cn.wolf.base.BaseTask;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.http.HttpRequest;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;
import cn.wolf.xml.Parser;

import com.spdg.ring.entity.ContentEntity;
import com.spdg.ring.resp.ContentResp;
import com.spdg.ring.xml.XmlParseFactory;

/**
 * <p>Title: 获取菜单项数据请求     </p>
 * <p>Description: 
 *  获取菜单项数据请求，除了伴手有礼
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-30             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class GetContentTask extends BaseTask<Void, Void, Void> {
	private String url;

	public GetContentTask(HttpCallBack<ContentResp> iCall, String url) {
		super(iCall);
		this.url = url;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected BaseResp doInBackground(Void... param) {
		HttpRequest request = new HttpRequest(ContentResp.class);
		ContentResp resp = new ContentResp();
		try {
			if (resp != null) {
				resp = (ContentResp) request.execute(url);
				String xmlStr = resp.getHttpResult();

				if (!StringUtil.isEmpty(xmlStr)) {
					InputStream in = new ByteArrayInputStream(xmlStr.trim().getBytes());

					Parser parser = XmlParseFactory.creator(XmlParseFactory.CONTENT_PARSER);
					List<ContentEntity> list = (List<ContentEntity>) parser.parse(in);
					if (list != null && list.size() > 0) {
						resp.setmContentEntity(list.get(0));
					}
				}
			}
		} catch (Exception ex) {
			Log.e("获取content item数据异常：" + ex);
		} finally {
			request.release();
		}

		return resp;
	}
}
